package com.leancloud.circle.adapter;

import android.content.DialogInterface;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSONArray;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.leancloud.circle.R;
import com.leancloud.circle.bean.CircleBean;
import com.leancloud.circle.manage.CircleBack;
import com.leancloud.circle.manage.CircleGlobal;
import com.vise.utils.view.DialogUtil;

import java.util.List;

import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.ToastUtil;
import tech.com.commoncore.view.SpaceGridDecoration;

import static com.leancloud.circle.manage.CirclePathManage.CIRCLE_ARTICLE_DETAILS;

public class MeCircleAdapter extends BaseQuickAdapter<CircleBean, BaseViewHolder> {

    public MeCircleAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final CircleBean item) {
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(CIRCLE_ARTICLE_DETAILS)
                        .withInt("communityId", item.getId())
                        .greenChannel()
                        .navigation();
            }
        });

        final RecyclerView imagesRecycler = helper.itemView.findViewById(R.id.circle_item_img_recycler);

        String date = item.getCreate_time();
        final String content = item.getPost_content();

        List<String> images = JSONArray.parseArray(item.getMore(), String.class);

        PhotoAdapter photoAdapter = new PhotoAdapter(R.layout.circle_layout_item_circle_photo);
        imagesRecycler.setAdapter(photoAdapter);
        imagesRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        int hSpace = (int) mContext.getResources().getDimension(R.dimen.dp_3);
        int vSpace = (int) mContext.getResources().getDimension(R.dimen.dp_1);
        SpaceGridDecoration decoration = new SpaceGridDecoration(hSpace, vSpace);
        imagesRecycler.addItemDecoration(decoration);
        photoAdapter.replaceData(images);
        photoAdapter.notifyDataSetChanged();

        helper.itemView.findViewById(R.id.circle_item_share_tx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FastUtil.startShareText(mContext, content);
            }
        });

        helper.setText(R.id.circle_item_date_tx, date);
        helper.setText(R.id.circle_item_content_tx, content);

        helper.itemView.findViewById(R.id.circle_item_remove_tx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.dialogBuilder(mContext, null, "您确认要删除吗?")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                CircleGlobal.getCircleImp().removeCircle(String.valueOf(item.getId()), new CircleBack() {
                                    @Override
                                    public void onBack(Object o, String err) {
                                        if (err == null) {
                                            remove(helper.getLayoutPosition());
                                        } else {
                                            ToastUtil.show("删除失败");
                                        }
                                    }
                                });
                            }
                        }).create().show();
            }
        });
    }
}
